package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Attendance;
import com.ruoyi.system.domain.bo.AttendanceBo;
import com.ruoyi.system.domain.vo.AttendanceVo;
import com.ruoyi.system.mapper.AttendanceMapper;
import com.ruoyi.system.service.IAttendanceService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 考勤到岗信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-11
 */
@RequiredArgsConstructor
@Service
public class AttendanceServiceImpl implements IAttendanceService {

    private final AttendanceMapper baseMapper;

    /**
     * 查询考勤到岗信息
     */
    @Override
    public AttendanceVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询考勤到岗信息列表
     */
    @Override
    public TableDataInfo<AttendanceVo> queryPageList(AttendanceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Attendance> lqw = buildQueryWrapper(bo);
        Page<AttendanceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询考勤到岗信息列表
     */
    @Override
    public List<AttendanceVo> queryList(AttendanceBo bo) {
        LambdaQueryWrapper<Attendance> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Attendance> buildQueryWrapper(AttendanceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Attendance> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getDepartment()), Attendance::getDepartment, bo.getDepartment());
        lqw.eq(bo.getDutyDate() != null, Attendance::getDutyDate, bo.getDutyDate());
        lqw.eq(StringUtils.isNotBlank(bo.getDutyType()), Attendance::getDutyType, bo.getDutyType());
        lqw.eq(StringUtils.isNotBlank(bo.getDutySubtype()), Attendance::getDutySubtype, bo.getDutySubtype());
        lqw.eq(StringUtils.isNotBlank(bo.getDutyLocation()), Attendance::getDutyLocation, bo.getDutyLocation());
        lqw.eq(bo.getScheduledOfficerId() != null, Attendance::getScheduledOfficerId, bo.getScheduledOfficerId());
        lqw.like(StringUtils.isNotBlank(bo.getShiftName()), Attendance::getShiftName, bo.getShiftName());
        lqw.eq(StringUtils.isNotBlank(bo.getShiftTimePeriod()), Attendance::getShiftTimePeriod, bo.getShiftTimePeriod());
        lqw.eq(bo.getCheckInTime() != null, Attendance::getCheckInTime, bo.getCheckInTime());
        lqw.eq(bo.getCheckOutTime() != null, Attendance::getCheckOutTime, bo.getCheckOutTime());
        lqw.eq(StringUtils.isNotBlank(bo.getCheckInStatus()), Attendance::getCheckInStatus, bo.getCheckInStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getCheckOutStatus()), Attendance::getCheckOutStatus, bo.getCheckOutStatus());
        return lqw;
    }

    /**
     * 新增考勤到岗信息
     */
    @Override
    public Boolean insertByBo(AttendanceBo bo) {
        Attendance add = BeanUtil.toBean(bo, Attendance.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改考勤到岗信息
     */
    @Override
    public Boolean updateByBo(AttendanceBo bo) {
        Attendance update = BeanUtil.toBean(bo, Attendance.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Attendance entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除考勤到岗信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
